import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';

import { Https } from '../../app/http.service'
/**
 * Generated class for the BuyPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
	selector: 'page-buy',
	templateUrl: 'buy.html',
})
export class BuyPage {
	data: string = '1'; 
	Url: string = new Https().Url + "act=goods&op=app_my_buy";
	list:Array<any>;
	page : number = 0;
	loadComplate:string = '0';
	constructor(public navCtrl: NavController, public navParams: NavParams, public toastCtrl: ToastController) {
	}

	ionViewDidLoad() {
		console.log('ionViewDidLoad BuyPage');
		this.loadData();
	}
	loadData(){
		fetch(this.Url, {
			method: 'post',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			body: "mid=" + localStorage.getItem("mid") + "&token=" + localStorage.getItem("token") + "&page=" + this.page,
		}).then((response) => {
			if (response) {
				if (response) {
					response.text().then((json) => {
						let data = JSON.parse(json).datas;
						if (data.code == 3) {
							this.list == null ? this.data = '0' : this.loadComplate = '1';
						} else {
							if (this.list == null){
								this.list = data
							}else{
								for(let item of data){
									this.list.push(item)
								}
							}
							console.log(this.list)
						}
					})
				} else {
					this.list == null ? this.data = '0' : this.data = '1';
				}

			} else {
				this.presentToast('出错');
			}

		}).catch((err) => {
			this.presentToast(err);
		})
	}

	doInfinite(infiniteScroll) {
		this.page = this.page + 10;
		console.log('Async operation has ended' + this.page);
		setTimeout(() => {
			this.loadData();
			infiniteScroll.complete();
		}, 2000);
		
	}
	presentToast(content) {
		let toast = this.toastCtrl.create({
			message: content,
			duration: 3000
		});
		toast.present();
	}

}
